<?php

$piArray = array("mainPage.php",   "subtaskController.php",
    "taskController.php", "charts.php", "createProject.php", "createProjectWithJs.php", "editProjectController.php",
    "piMain.php", "populateDropdown.php", "projectSubmit.php", "unknownRole.php", "utility.php", "viewReqDetails.php",
    "viewMyProjects.php", "projectController.php", "subtaskController.php", "taskController.php",
    "nonLaborController.php", "contractController.php","login_page.php");

$piReviewArray = array("mainPage.php",  "subtaskController.php",
    "taskController.php", "charts.php", "createProject.php", "createProjectWithJs.php", "editProjectController.php",
    "piMain.php", "populateDropdown.php", "projectSubmit.php", "unknownRole.php", "utility.php", "viewReqDetails.php",
    "viewMyProjects.php", "projectController.php", "viewProjects.php", "subtaskController.php", "taskController.php",
    "nonLaborController.php", "contractController.php","login_page.php");

$piApproverArray = array("mainPage.php", "subtaskController.php",
    "taskController.php", "charts.php", "createProject.php", "createProjectWithJs.php", "editProjectController.php",
    "piMain.php", "populateDropdown.php", "projectSubmit.php", "unknownRole.php", "utility.php", "viewReqDetails.php",
    "viewMyProjects.php", "projectController.php", "viewProjects.php", "subtaskController.php", "taskController.php",
    "nonLaborController.php", "contractController.php", "portfolioController.php","login_page.php");

$cnmocUserArray = array("mainPage.php", "editDB.php", "editDBSubmit.php", "populateDropdown.php","utility.php", "viewReqDetails.php","login_page.php");

function isAuthorized($pageName) {
    if (!isset($_SESSION)) {
        session_start();
    }
    if (isset($_SESSION['role'])) {
        $role = $_SESSION['role'];
        //
        if (strcmp($role, "pi") === 0) {
            global $piArray;
            return valueExists($piArray, $pageName);
        } else if (strcmp($role, "admin") === 0) {
            return true;
        } else if (strcmp($role, "pireviewer") === 0) {
            global $piReviewArray;
            return valueExists($piReviewArray, $pageName);
        } else if (strcmp($role, "piapprover") === 0) {
            global $piApproverArray;
            return valueExists($piApproverArray, $pageName);
        }else if (strcmp($role, "cnmocUser") === 0) {
            global $cnmocUserArray;
            return valueExists($cnmocUserArray, $pageName);
        }else {
            return false;
        }
    } else {
        //echo "Role not set in session <br>";
        return false;
    }
}

function valueExists($array, $value) {
    //echo sizeof($array).'<br>';
    for ($i = 0; $i < sizeof($array); $i++) {
        //echo $array[$i].'<br>';
        if (strcmp($value, $array[$i]) === 0) {
            //echo '1 '.$value.' 2 '.$array[$i];
            return true;
        }
    }
    return false;
}

?>
